package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.dto.ArticleStreamMessage;
import com.heima.article.service.IHotArticleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class HotArticleResultListener {
    @Autowired
    private IHotArticleService iHotArticleService;

    @KafkaListener(topics = {"${topic.hot_article_result_topic}"})
    public void handleMsg(ConsumerRecord<String,String> record){
        String value = record.value();
        if (StringUtils.isNotEmpty(value)){
            ArticleStreamMessage articleStreamMessage = JSON.parseObject(value, ArticleStreamMessage.class);
            //调用热点文章服务更新数据
            iHotArticleService.update(articleStreamMessage);
        }
    }
}
